package com.comdo.newup_loan.module.PresentRecord;

import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.http.RetrofitHelper;
import com.comdo.newup_loan.model.PresentRecordBean;
import com.comdo.newup_loan.model.Result;
import com.comdo.newup_loan.util.StringUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

/**
 * Created by liyuan on 2017/11/7.
 */

class PresentRecordPersenter extends PresentRecordContact.Presenter{
    @Override
    public void getList(int page) {
        Gson gson=new Gson();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("userId",Config.loginInfo.getUserId());
        map.put("page",page);
        map.put("token", Config.loginInfo.getToken());
        map.put("sign", StringUtils.SortStringAndAddKey(map));
        String strEntity = gson.toJson(map);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        mCompositeDisposable.add(RetrofitHelper.createMyApi().getAppTakeoutRecordList(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<List<PresentRecordBean>>>() {
                    @Override
                    public void accept(Result<List<PresentRecordBean>> Result) throws Exception {
                        if(Result.getCode()==1){
                            mView.getList(Result.getResult());
                        }else if(Result.getCode()==-3){
                            mView.getFail("您的登陆已经失效，请重新登陆");
                            mView.exitLogin();
                        }else{
                           mView.getFail(Result.getMessage());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.getFail("请求失败");
                    }
                }));
    }
}
